import { ChatOllama } from "@langchain/ollama";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { RunnableLambda } from "@langchain/core/runnables";

// 创建模型
const model = new ChatOllama({
  model: "llama3",
  temperature: 0.7,
});

// 解析器
const parser = new StringOutputParser();

// 创建两个子链
const chain1 = PromptTemplate.fromTemplate(
  "请用中文用 2-3 句概括以下主题的核心含义：{input}"
)
  .pipe(model)
  .pipe(parser);

const chain2 = PromptTemplate.fromTemplate(
  "请用中文从以下主题中提取 5 个关键词，以逗号分隔：{input}"
)
  .pipe(model)
  .pipe(parser);

let chain = RunnableLambda.from((x) => x);

chain = chain.pipe({
  summary: chain1,
  keywords: chain2,
});

const res = await chain.invoke({
  input: "闭包",
});
console.log(res);
